﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CakeAnimation;
using System.Xml;
using System.IO;
using CharacterAnimationEditor;

namespace CakeWPF
{
    class Project
    {
        public List<Animation> Animations { get; set; }

        public void Load(Context context, string filename)
        {
            string folder = System.IO.Path.GetDirectoryName(filename);

            LoadAnimationList(context, folder);

            LoadProjectFile(context, filename);
        }

        private void LoadProjectFile(Context context, string filename)
        {


            XmlDocument doc = new XmlDocument();
            XmlReader reader = new XmlTextReader(filename);
            doc.Load(reader);
            XmlElement elem = doc.GetElementById("CakeProject");
            XmlNode elem2 = doc.FirstChild.NextSibling.SelectSingleNode("Animations");

            foreach (XmlElement animNode in elem2.ChildNodes)
            {
                string name = animNode.Name;
                foreach (Animation anim in Animations)
                {
                    if (anim.Name == name)
                    {
                        if (animNode.HasAttribute("loopable"))
                        {
                            anim.Loopable = bool.Parse(animNode.Attributes["loopable"].Value);
                        }

                        foreach (XmlElement boneNode in animNode.ChildNodes)
                        {
                            if (boneNode.HasAttribute("filtered"))
                            {
                                foreach (KeyFrame kf in anim.m_Keyframes)
                                {
                                    Bone b = kf.Pose.GetBone(boneNode.Name.ToString());
                                    b.Filtered = bool.Parse(boneNode.Attributes["filtered"].Value);
                                }
                            }
                        }
                    }
                }
            }

            reader.Close();
        }

        private void LoadAnimationList(Context context, string folder)
        {
            Animations = new List<Animation>();

            AnimationSerializer s = new AnimationSerializer();
            string[] files = Directory.GetFiles(folder);
            foreach (string f in files)
            {
                if (f.EndsWith(".anim"))
                {
                    FileStream stream = new FileStream(f, FileMode.Open);
                    Animation anim = s.LoadAnimation(context.AnimationContext, f, stream, context.CurrentKeyFrame.Pose);
                    Animations.Add(anim);
                }
            }
        }

        public void Save(string filename)
        {

            XmlDocument doc = new XmlDocument();
            XmlElement root = doc.CreateElement("CakeProject");
            doc.AppendChild(root);
            XmlElement animationsElem = doc.CreateElement("Animations");
            root.AppendChild(animationsElem);

            foreach (Animation anim in Animations)
            {
                XmlElement elem = doc.CreateElement(anim.Name);
                animationsElem.AppendChild(elem);
                elem.SetAttribute("loopable", anim.Loopable.ToString());
                KeyFrame kf = anim.GetLastKeyframe();
                foreach (Bone bone in kf.Pose.GetBoneEnumeration())
                {
                    if (bone.Filtered)
                    {
                        XmlElement boneelem = doc.CreateElement(bone.Name);
                        elem.AppendChild(boneelem);
                        boneelem.SetAttribute("filtered", true.ToString());
                    }
                }
            }

            XmlElement previewsElem = doc.CreateElement("Previews");
            root.AppendChild(previewsElem);

            XmlTextWriter writer = new XmlTextWriter(filename, Encoding.UTF8);
            writer.Formatting = Formatting.Indented;

            doc.Save(writer);
            writer.Close();
        }
    }
}
